package cn.com.hhrcw.sms.service;

/**
 * @author yyg
 * @date 2023年01月14日
 */

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.*;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;

@Slf4j
public class SendMain {
    private static final String appKey = "DDdhylKp";
    private static final String appSecret = "0105df3ae0070b133722ad8a4d6a6e6b38aa6810";


    public static void main(String [] arg){
        TreeMap<String,String> treeMap = new TreeMap<>();
        treeMap.put("appKey",appKey);
        treeMap.put("mobiles","17888843296");
        treeMap.put("smsContent","验证码：000999，5分钟内有效。欢迎登录黄河人才网！");
        treeMap.put("timestamp",String.valueOf(System.currentTimeMillis()));
        treeMap.put("nonce",uuid());
        treeMap.put("signatureMethod","HMAC-SHA1");
        String signature = getSignature(treeMap);
        treeMap.put("signature",signature);

        HttpResponse response = HttpUtil.createPost("https://ping.hn-talent.com/send/v1/sendSms").contentType("application/json").body(JSON.toJSONString(treeMap)).execute();
        System.out.println(">>>>>>>>>>>>>>>>>>>>>"+response.body());
        SendResultVo resultVo = JSON.parseObject(response.body(),SendResultVo.class);
        System.out.println(">>>>>>>>>>>>>>>>>>>>> code "+ resultVo.getCode());
        System.out.println(">>>>>>>>>>>>>>>>>>>>>OK");
    }
    public static String sendSmsV1(String smsContent,String mobile) {
            TreeMap<String,String> treeMap = new TreeMap<>();
            treeMap.put("appKey",appKey);
            treeMap.put("mobiles",mobile);
            treeMap.put("smsContent",smsContent);
            treeMap.put("timestamp",String.valueOf(System.currentTimeMillis()));
            treeMap.put("nonce",uuid());
            treeMap.put("signatureMethod","HMAC-SHA1");
            String signature = getSignature(treeMap);
            treeMap.put("signature",signature);

            HttpResponse response = HttpUtil.createPost("https://ping.hn-talent.com/send/v1/sendSms").contentType("application/json").body(JSON.toJSONString(treeMap)).execute();
            log.info("mobile:{} >>>>>>>>>> content:{} >>>>>>>>>>> response:{}",mobile ,smsContent, response.body());
            SendResultVo resultVo = JSON.parseObject(response.body(),SendResultVo.class);
            log.info("{}>>>>>>>>>>>>>>>>>>>>>OK",mobile);
            return resultVo.getCode();
    }
    public static String uuid(){
        String s= UUID.randomUUID().toString();
        return s.replace("-", "");
    }

    public static String getSignature(TreeMap<String,String> treeMap){
        List<String> valuesList = new ArrayList<>();
        for(Map.Entry<String,String> entry:treeMap.entrySet()){
            valuesList.add(entry.getValue());
        }
        String values = String.join("&",valuesList);
        try{
            String encodedValues = URLEncoder.encode(values,"UTF-8");
            Mac mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec spec = new SecretKeySpec(appSecret.getBytes(), "HmacSHA1");
            mac.init(spec);
            byte[] byteHMAC = mac.doFinal(encodedValues.getBytes());
            Base64 base64 = new Base64();
            return base64.encodeAsString(byteHMAC);
        }
        catch (Exception e){
            return null;
        }
    }
}
